import requests
import json
import datetime
from datetime import timedelta


def get_session():
    '''
    获取OA账号登陆session
    :return:
    '''
    today = datetime.datetime.now().strftime("%Y-%m-%d")
    login_url = 'https://auth.xincheng.com/LoginService.ashx'
    login_payload = {
        'lang': 'en - us',
        'systemCode': 'A07',
        'rememberPassword': 'false',
        'challengeNumber': '',
        'randomDate': '2021-05-23',
        '15': '34:50',
        'hidetxtPassword': 'lU/Q3OulShPF0iazGNk5O7YGzt9cMlFGiz5l/m73bCJ9WSVGCtqmnHroP6IjSat4m7HTQlMn9VGVbAssdYyR4BOsEdjB3lTY4wmzqd8gfwV5YpbaeoERAzuwpl7QzCyjEnV34Nu7eMlyXZrJLxdGLEdVxdKetMMhJlT2y5sKL1SrpPW4i5sJRC2uH2Jh2qeWkOEW87C8QF9Tyr+Hhyx8ZjH0RbtKW82gLG+edUxls2nXZ6rMkZHmOh9QrPc2/ORlIYAHItYHyd098/6+afAQavKHcmh8kGmmNW0uZYOs57ZJxwLpJ2NoDOX+YWgS57Wh6ceQXqZR3Gt7b+dDN7M1gw==',
        'txtIsTrustAccessor': 'True',
        'txtUserName': 'wangrui20',
    }
    login_header = {
        'Referer': 'https://auth.xincheng.com/?challengeNumber=ZGY5eGpsaGt5NTB0cWRlcHJ0c2h1OXVuMDBlMHFnbXMwN2JmZGEzYmEyNTlsaHFtYXN5MHlheWRjOGNsZmIyZjF4ejJ0b3BvbW1mMHl4ZTR4b2ZlcnlrdGVsemRncG1yY3MybzVicmxyOHlqdDRsbTgzb2I5d2tpOGx3dnlvMW9zOXJ6azh4OWRoYnVkd253b2pubHNpbmN2Y3ZvOWttNjBtcW14MWV2NDFnOHpmc2s4bHN2ZmN3aGk0Y2RhZXR4c2JtbjJmYmZ6cWV1MWlibnE2OGUyOTd4azBpMGd5ODlrMmFqM2w3djMxb2d5c3N1bnFnYWFjNHV6YWluZWY5cA==&flag=3&systemCode=A07&RetutnUrl=http%3A%2F%2Fekp.xincheng.com%2Fsys%2Fportal%2Fpage.jsp',
        'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.89 Safari/537.36',
    }
    session = requests.session()
    session.post(login_url, data=login_payload, headers=login_header)
    index_url = 'http://worklog.xincheng.com:8078/web/Home/Report'
    session.get(index_url)
    return session


def get_log_data():
    '''
    获取未填写日志人员名单
    :return:
    '''
    # 本周第一天和最后一天
    now = datetime.datetime.now()
    this_week_start = now - timedelta(days=now.weekday())
    this_week_end = now + timedelta(days=6 - now.weekday())
    index_url = 'http://worklog.xincheng.com:8078/web/Home/GetUserReportDataByDept'
    payload = 'deptId=0551a828bf1e11eabfe3efeb90092519&deptName=%E5%BE%90%E5%B7%9E%E5%8C%BA%E5%9F%9F&startDate=' + str(
        this_week_start.strftime('%Y-%m-%d')) + '&endDate=' + str(this_week_end.strftime('%Y-%m-%d')) + '&type=0'
    headers = {
        'Accept': 'application/json, text/javascript, */*; q=0.01',
        'X-Requested-With': 'XMLHttpRequest',
        'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 11_2_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.114 Safari/537.36',
        'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
    }
    req = get_session().post(index_url, headers=headers, data=payload)
    req.content.decode()
    req = json.loads(req.text)
    req = req['items']
    job_user_name = []
    count = 0
    for i in req:
        full_dep = i['FullDeptName']
        if "连云港赣榆商管公司" in full_dep:
            count = count + 1
            job_info = {"user_name": i['userName'], "ActualdayCount": i['ActualdayCount'],
                        "ShouldDayCount": i['ShouldDayCount']}
            job_user_name.append(job_info)
    return job_user_name, count
